//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package cn.zd.util;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Locale;
import java.util.TimeZone;

import cn.hutool.core.date.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtil {
    private static final Logger log = LoggerFactory.getLogger(DateUtil.class);
    public static final long MS = 1L;
    public static final int UTC_8_HOUR = 8;
    public static final long SECOND_MS = 1000L;
    public static final long MINUTE_MS = 60000L;
    public static final long HOUR_MS = 3600000L;
    public static final long DAY_MS = 86400000L;
    public static final String NORM_DATE_PATTERN = "yyyy-MM-dd";
    public static final String SHORT_DATE_PATTERN = "yyyyMMdd";
    public static final String SHORT_DATE_HOUR_PATTERN = "yyyyMMddHH";
    public static final String NORM_TIME_PATTERN = "HH:mm:ss";
    public static final String NORM_DATETIME_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String NORM_DATETIME_PATTERN_NO_SPLIT = "yyyyMMddHHmmss";
    public static final String NORM_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String HTTP_DATETIME_PATTERN = "EEE, dd MMM yyyy HH:mm:ss z";
    public static final String SHORT_YEAR_MONTH_PATTERN = "yyyyMM";
    private static final ThreadLocal<DateFormat> format = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    });
    private static final ThreadLocal<SimpleDateFormat> NORM_DATE_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd");
    });
    private static final ThreadLocal<SimpleDateFormat> NORM_TIME_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("HH:mm:ss");
    });
    private static final ThreadLocal<SimpleDateFormat> NORM_TIME_MINUTE_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm");
    });
    private static final ThreadLocal<SimpleDateFormat> NORM_DATETIME_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    });
    private static final ThreadLocal<SimpleDateFormat> HTTP_DATETIME_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    });
    private static final ThreadLocal<SimpleDateFormat> SHORT_DATE_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyyMMdd");
    });
    private static final ThreadLocal<SimpleDateFormat> SHORT_YEAR_MONTH_FORMAT = ThreadLocal.withInitial(() -> {
        return new SimpleDateFormat("yyyyMM");
    });

    public DateUtil() {
    }

    public static String now() {
        return formatDateTime(new DateTime());
    }

    public static String nowMi() {
        return formatDateMinute(new DateTime());
    }

    public static String today() {
        return formatDate(new DateTime());
    }

    public static DateTime date() {
        return new DateTime();
    }

    public static DateTime date(long date) {
        return new DateTime(date);
    }

    public static String yearAndSeason(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return yearAndSeason(cal);
    }

    public static LinkedHashSet<String> yearAndSeasons(Date startDate, Date endDate) {
        LinkedHashSet<String> seasons = new LinkedHashSet();
        if (startDate != null && endDate != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(startDate);

            while (true) {
                if (startDate.after(endDate)) {
                    startDate = endDate;
                }

                seasons.add(yearAndSeason(cal));
                if (startDate.equals(endDate)) {
                    return seasons;
                }

                cal.add(2, 3);
                startDate = cal.getTime();
            }
        } else {
            return seasons;
        }
    }

    public static String format(Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static String formatDateTime(Date date) {
        return ((SimpleDateFormat)NORM_DATETIME_FORMAT.get()).format(date);
    }

    public static String formatDateMinute(Date date) {
        return ((SimpleDateFormat)NORM_TIME_MINUTE_FORMAT.get()).format(date);
    }

    public static String shortDate(Date date) {
        return ((SimpleDateFormat)SHORT_DATE_FORMAT.get()).format(date);
    }

    public static String formatDate(Date date) {
        return ((SimpleDateFormat)NORM_DATE_FORMAT.get()).format(date);
    }

    public static String formatHttpDate(Date date) {
        return ((SimpleDateFormat)HTTP_DATETIME_FORMAT.get()).format(date);
    }

    public static DateTime parse(String dateStr, SimpleDateFormat simpleDateFormat) {
        try {
            return new DateTime(simpleDateFormat.parse(dateStr));
        } catch (Exception var3) {
            throw new RuntimeException("");
        }
    }

    public static DateTime parse(String dateString, String format) {
        return parse(dateString, new SimpleDateFormat(format));
    }

    public static DateTime parseDateTime(String dateString) {
        return parse(dateString, (SimpleDateFormat)NORM_DATETIME_FORMAT.get());
    }

    public static DateTime parseDate(String dateString) {
        return parse(dateString, (SimpleDateFormat)NORM_DATE_FORMAT.get());
    }

    public static DateTime parseTime(String timeString) {
        return parse(timeString, (SimpleDateFormat)NORM_TIME_FORMAT.get());
    }

    public static DateTime parse(String dateStr) {
        if (null == dateStr) {
            return null;
        } else {
            dateStr = dateStr.trim();
            int length = dateStr.length();

            try {
                if (length == "yyyy-MM-dd HH:mm:ss".length()) {
                    return parseDateTime(dateStr);
                }

                if (length == "yyyy-MM-dd".length()) {
                    return parseDate(dateStr);
                }

                if (length == "HH:mm:ss".length()) {
                    return parseTime(dateStr);
                }

                if (length == "yyyy-MM-dd HH:mm".length()) {
                    return parse(dateStr, "yyyy-MM-dd HH:mm");
                }

                if (length == "yyyy-MM-dd HH:mm:ss.SSS".length()) {
                    return parse(dateStr, "yyyy-MM-dd HH:mm:ss.SSS");
                }
            } catch (Exception var3) {
                log.info("date format error", var3);
                throw new RuntimeException("ResponseCode.UNKNOWN_ERROR");
            }

            throw new RuntimeException("ResponseCode.UNKNOWN_ERROR");
        }
    }

    public static DateTime getBeginTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getEndTimeOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getBeginTimeOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(date);
        calendar.set(7, 2);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getEndTimeOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(date);
        calendar.set(7, 1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getBeginTimeOfMonth(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, calendar2.get(1));
        calendar.set(2, calendar2.get(2));
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getEndTimeOfMonth(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, calendar2.get(1));
        calendar.set(2, calendar2.get(2));
        calendar.roll(5, -1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getBeginTimeOfQuarter(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, calendar2.get(1));
        calendar.set(2, calendar2.get(2) / 3 * 3);
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getEndTimeOfQuarter(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, calendar2.get(1));
        calendar.set(2, calendar2.get(2) / 3 * 3 + 2);
        calendar.roll(5, -1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getBeginTimeOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.clear();
        calendar2.set(1, calendar.get(1));
        calendar2.set(6, 1);
        calendar2.set(11, 0);
        calendar2.set(12, 0);
        calendar2.set(13, 0);
        calendar2.set(14, 0);
        return new DateTime(calendar2.getTime());
    }

    public static DateTime getEndTimeOfYear(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(1, calendar2.get(1));
        calendar.roll(6, -1);
        calendar.set(11, 23);
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getBeginTimeOfHour(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, calendar2.get(11));
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new DateTime(calendar.getTime());
    }

    public static DateTime getEndTimeOfHour(Date date) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(11, calendar2.get(11));
        calendar.set(12, 59);
        calendar.set(13, 59);
        calendar.set(14, 999);
        return new DateTime(calendar.getTime());
    }

    public static DateTime yesterday() {
        return offsiteDay(new DateTime(), -1);
    }

    public static DateTime dayBeforeYesterday() {
        return offsiteDay(new DateTime(), -2);
    }

    public static DateTime lastWeek() {
        return offsiteWeek(new DateTime(), -1);
    }

    public static DateTime lastMouth() {
        return offsiteMonth(new DateTime(), -1);
    }

    public static DateTime offsiteByUnit(Date date, int offsite, Integer unit) {
        return offsiteDate(date, unit, offsite);
    }

    public static DateTime offsiteDay(Date date, int offsite) {
        return offsiteDate(date, 6, offsite);
    }

    public static DateTime offsiteYear(Date date, int offsite) {
        return offsiteDate(date, 1, offsite);
    }

    public static DateTime offsiteWeek(Date date, int offsite) {
        return offsiteDate(date, 3, offsite);
    }

    public static DateTime offsiteMonth(Date date, int offsite) {
        return offsiteDate(date, 2, offsite);
    }

    public static DateTime offsiteDate(Date date, int calendarField, int offsite) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(calendarField, offsite);
        return new DateTime(cal.getTime());
    }

    public static long diff(Date subtrahend, Date minuend, long diffField) {
        long diff = minuend.getTime() - subtrahend.getTime();
        return diff / diffField;
    }

    public static long spendNt(long preTime) {
        return System.nanoTime() - preTime;
    }

    public static long spendMs(long preTime) {
        return System.currentTimeMillis() - preTime;
    }

    public static int toIntSecond(Date date) {
        return Integer.parseInt(format(date, "yyMMddHHmm"));
    }

    public static int weekCount(Date start, Date end) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        int startWeekofYear = startCalendar.get(3);
        int endWeekofYear = endCalendar.get(3);
        int count = endWeekofYear - startWeekofYear + 1;
        if (1 != startCalendar.get(7)) {
            --count;
        }

        return count;
    }

    private static String yearAndSeason(Calendar cal) {
        return "" + cal.get(1) + (cal.get(2) / 3 + 1);
    }

    public static String diyDate() {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        return sdf.format(now);
    }

    public static boolean betweenTimeRange(Date time, Date startTime, Date endTime) {
        return time.getTime() >= startTime.getTime() && time.getTime() <= endTime.getTime();
    }

    public static Timestamp getUTCTimestamp() {
        Timestamp ts = new Timestamp(getUTCDateTime().getTime());
        return ts;
    }

    public static DateTime getUTCDateTime() {
        return new DateTime();
    }

    private static String getUTCTimeStr() {
        StringBuffer UTCTimeBuffer = new StringBuffer();
        Calendar cal = Calendar.getInstance();
        int zoneOffset = cal.get(15);
        int dstOffset = cal.get(16);
        cal.add(14, -(zoneOffset + dstOffset));
        int year = cal.get(1);
        int month = cal.get(2) + 1;
        int day = cal.get(5);
        int hour = cal.get(11);
        int minute = cal.get(12);
        int second = cal.get(13);
        int millsecond = cal.get(14);
        UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day);
        UTCTimeBuffer.append(" ").append(hour).append(":").append(minute).append(":").append(second).append(".")
            .append(millsecond);

        try {
            ((DateFormat)format.get()).parse(UTCTimeBuffer.toString());
            return UTCTimeBuffer.toString();
        } catch (ParseException var12) {
            log.error(var12.getMessage(), var12);
            return null;
        }
    }

    public static String nowMonth() {
        return ((SimpleDateFormat)SHORT_YEAR_MONTH_FORMAT.get()).format(new DateTime());
    }

    public static String getUTCTimeFromSys(Date date) {
        StringBuffer UTCTimeBuffer = new StringBuffer();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int zoneOffset = cal.get(15);
        int dstOffset = cal.get(16);
        cal.add(14, -(zoneOffset + dstOffset));
        int year = cal.get(1);
        int month = cal.get(2) + 1;
        int day = cal.get(5);
        int hour = cal.get(11);
        int minute = cal.get(12);
        int second = cal.get(13);
        int millsecond = cal.get(14);
        UTCTimeBuffer.append(year).append("-").append(month).append("-").append(day);
        UTCTimeBuffer.append(" ").append(hour).append(":").append(minute).append(":").append(second).append(".")
            .append(millsecond);

        try {
            ((DateFormat)format.get()).parse(UTCTimeBuffer.toString());
            return UTCTimeBuffer.toString();
        } catch (ParseException var13) {
            log.error(var13.getMessage(), var13);
            return null;
        }
    }

    public static Timestamp getTimestampAfterTransfer(Date date) {
        try {
            DateFormat formatConfirmDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            formatConfirmDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date dateUtc = formatConfirmDate.parse(getUTCTimeFromSys(date));
            return new Timestamp(dateUtc.getTime());
        } catch (ParseException var3) {
            log.error(var3.getMessage(), var3);
            return null;
        }
    }

    public static Date getTimestampNotTransfer(Date sourceDate) {
        try {
            DateFormat sourceformatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            String strSourceDate = sourceformatDate.format(sourceDate);
            DateFormat formatConfirmDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            formatConfirmDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            Date dateUtc = formatConfirmDate.parse(strSourceDate);
            return dateUtc;
        } catch (ParseException var5) {
            log.error(var5.getMessage(), var5);
            return null;
        }
    }

    public static long diffDays(Date startDate, Date endDate) {
        return diff(startDate, endDate, 86400000L);
    }

    public static long getQueryDateEndTIme(long date) {
        DateTime dateTime = new DateTime(date);
        DateTime newDt = offsiteDay(dateTime, 1);
        newDt = offsiteByUnit(newDt, 1, 13);
        Long endTime = newDt.getTime();
        return endTime;
    }

    public static Long getTime(Date realDate, Date expectDate) {
        return realDate == null ? expectDate.getTime() : realDate.getTime();
    }

    public static LocalDate getLocalDate(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.of("UTC");
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalDate localDate = localDateTime.toLocalDate();
        return localDate;
    }

    public static LocalTime getLocalTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zone = ZoneId.of("UTC");
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
        LocalTime localTime = localDateTime.toLocalTime();
        return localTime;
    }

    public static Long convertToTimestamp(Date date) {
        return date.getTime() / 1000L;
    }

    public static Integer currentDate() {
        return Integer.parseInt(((SimpleDateFormat)SHORT_DATE_FORMAT.get()).format(date()));
    }

    public static Date currentTime(Date date) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date());
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date);
        calendar2.set(1, calendar1.get(1));
        calendar2.set(2, calendar1.get(2));
        calendar2.set(5, calendar1.get(5));
        return calendar2.getTime();
    }

    public static Integer lastDate(Integer date) {
        try {
            Date last = offsiteDay(((SimpleDateFormat)SHORT_DATE_FORMAT.get()).parse(date.toString()), -1);
            return Integer.parseInt(((SimpleDateFormat)SHORT_DATE_FORMAT.get()).format(last));
        } catch (ParseException var2) {
            log.error(var2.getMessage(), var2);
            return null;
        }
    }

    public static Integer nextDate(Integer date) {
        try {
            Date next = offsiteDay(((SimpleDateFormat)SHORT_DATE_FORMAT.get()).parse(date.toString()), 1);
            return Integer.parseInt(((SimpleDateFormat)SHORT_DATE_FORMAT.get()).format(next));
        } catch (ParseException var2) {
            log.error(var2.getMessage(), var2);
            return null;
        }
    }
}
